﻿using Gnivc.CarDealership.Api.Application.Abstractions;
using Gnivc.CarDealership.Api.Application.Abstractions.Repositories;
using Gnivc.CarDealership.Api.Infrastructure.Persistence;
using Gnivc.CarDealership.Api.Infrastructure.Repositories;
using Microsoft.EntityFrameworkCore;

namespace Gnivc.CarDealership.Api;

public static class DependencyInjection
{
    private const string ConnectionStringName = "ApplicationDatabase";

    public static IServiceCollection AddInfrastructure(this IServiceCollection services,
        IConfiguration configuration)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
        {
            options.UseNpgsql(configuration.GetConnectionString(ConnectionStringName));
        });
        services.AddScoped<IApplicationDbContext, ApplicationDbContext>();
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        services.AddRepositories();
        return services;
    }

    private static IServiceCollection AddRepositories(this IServiceCollection services)
    {
        services.AddTransient<IBrandRepository, BrandRepository>();
        services.AddTransient<IModelRepository, ModelRepository>();
        services.AddTransient<IPriceRepository, PriceRepository>();
        services.AddTransient<IReservationRepository, ReservationRepository>();
        services.AddTransient<IVehicleRepository, VehicleRepository>();
        services.AddTransient<IUserRepository, UserRepository>();
        return services;
    }
}
